<?php
/**
 * Create by 11li
 * Date 2020/3/9
 * Time 11:28
 *
 */

namespace app\common\service;


use app\common\model\Video;
use app\common\model\VideoLike;
use think\Exception;

class LikeService extends BaseService
{
	public function __construct()
	{
		$this->model = new VideoLike();
	}
	
	/**
	 * 添加视频浏览记录
	 * @param $userId
	 * @param $videoId
	 * @return bool
	 * @throws Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function addViewRecord($userId, $videoId) {
		if(!$userId || !$videoId) {
			throw new Exception('参数错误');
		}
		
		$data = [
			'user_id' => $userId,
			'video_id' => $videoId,
			'type' => StatusService::VIDEO_TYPE_VIEW
		];
		
		$row = $this->model
			->where($data)
			->find();
		
		if(!$row) {
			$res = $this->model->isUpdate(false)
				->save($data);
			if(!$res) {
				throw new Exception('添加浏览记录出错');
			}
			
			$numberRes = (new Video())
				->where(['id' => $videoId])
				->setInc('view_number');
			
		}
		
		return true;
	}
}